package com.bbx.taxi.client.Util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import com.umeng.socialize.utils.Log;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageStats;
import android.os.Looper;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.widget.Toast;

public class AppStarted {
	/**检测程序运行*/ 
	public static boolean isServiceStarted(Context context, String PackageName) {
		boolean isStarted = false;
		try {
			ActivityManager mActivityManager = (ActivityManager) context
					.getSystemService(Context.ACTIVITY_SERVICE);
			List<RunningAppProcessInfo> run = mActivityManager
					.getRunningAppProcesses();
			for (ActivityManager.RunningAppProcessInfo pro : run) {
				if (pro.processName.equals(PackageName)) {
					isStarted = true;
					break;
				}
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		}
		return isStarted;
	}
	/**
	 * 判断App是否在前台运行
	 * @return
	 */
	public static boolean isRunningForeground (Context context)  
	{  
	    ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);  
	    ComponentName cn = am.getRunningTasks(1).get(0).topActivity;  
	    String currentPackageName = cn.getPackageName();  
	    if(!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(context.getPackageName()))  
	    {  
	        return true ;  
	    }  
	   
	    return false ;  
	}  
	// 获取程序自身包名
	public static String getAppPackageName(Context context) {
		try {
			String pkName = context.getPackageName();
			return pkName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
